package wviz.docburst.render;

import prefuse.render.AbstractShapeRenderer;
import prefuse.visual.VisualItem;
import wviz.docburst.geom.Sector2D;

import java.awt.*;


/**
 * Renders a Sector2D shape for a node.
 * Assumes the columns innerRadius, outerRadius, startAngle, and angleExtent are already in the VisualItem schema.
 * They are added by StarburstLayout, but if that layout isn't being used, they need to be added and set in another way.
 *
 * @author <a href="http://www.cs.utoronto.ca/~ccollins">Christopher Collins</a>
 * @version 1.0
 */
public class SectorRenderer extends AbstractShapeRenderer {

    /**
     * The shape instance to use for rendering.  prefuse rendering recommends a single shape
     * instance reused to minimize instantiation/garbage collection.
     */
    private Sector2D sector2D = new Sector2D();

    /**
     * Renderer for Sector2D nodes (wedges from an annulus).
     */
    public SectorRenderer() {
        super();
    }

    /**
     * Using the paramters stored in the VisualItem, set the position and size of the sector2d.
     * Assumes the columns innerRadius, outerRadius, startAngle, and angleExtent are already in the VisualItem schema.
     * They are added by StarburstLayout, but if that layout isn't being used, they need to be added and set in another way.
     *
     * @param item the VisualItem to set the shape for
     * @return the shape for the given visual item
     */
    protected Shape getRawShape(VisualItem item) {
        double x = item.getX();
        if (Double.isNaN(x) || Double.isInfinite(x))
            x = 0;
        double y = item.getY();
        if (Double.isNaN(y) || Double.isInfinite(y))
            y = 0;
        if (item.getColumnIndex("innerRadius") == -1) {
            System.out.println("Warning, no column 'innerRadius' in item ");
            for(int i=0; i< item.getColumnCount();i++) {
                System.out.println("item.getColumnName(i) = " + item.getColumnName(i));
            }
        }
        double innerRadius = item.getDouble("innerRadius");
        double outerRadius = item.getDouble("outerRadius");
        double startAngle = item.getDouble("startAngle");
        double angleExtent = item.getDouble("angleExtent");
        sector2D.setSectorByCenter(x, y, innerRadius, outerRadius, startAngle, angleExtent);
        return sector2D;
    }
}
